#include "iostream"
#include "LutIgnXmlFile.h"
#include "tinyxml.h"

using namespace tinyxml;

LutIgnXmlFile::LutIgnXmlFile()
{
}

void
LutIgnXmlFile::open(std::string filename)
{
    //std::cout<<filename.c_str()<<std::endl;
    /*FILE* file=fopen(filename.c_str(), "rb" );
    if(!file)
    {
        std::cout<<"pb open file rb"<<std::endl;
    }*/
    TiXmlDocument doc(filename.c_str());
    if(!doc.LoadFile()){
            std::cerr << "error #" << doc.ErrorId() << " : " << doc.ErrorDesc()
                      << std::endl;
    }
    else{
        m_filename=filename;
    }
}

boost::shared_ptr<LutModel>
LutIgnXmlFile::loadLut()
{

    boost::shared_ptr<LutModel> lutmodel_ptr=boost::shared_ptr<LutModel>() ;
    TiXmlDocument doc(m_filename.c_str());
    if(!doc.LoadFile()){
            std::cerr<< "erreur lors du chargement" << std::endl;
            std::cerr << "error #" << doc.ErrorId() << " : " << doc.ErrorDesc()
                      << std::endl;
    }

    // read file
    TiXmlHandle hdl(&doc);
    TiXmlElement* pElem;
    pElem=hdl.FirstChild("lut").Element();
    // iterate on xml nodes
    //--root lut node
    if(!pElem){
       std::cout << " lut node doesn't exist " << std::endl;
       return lutmodel_ptr;
    }
    std::string systCoulstring = pElem->Attribute("systemeCouleur");
    if(systCoulstring == "RVB")
    {
        lutmodel_ptr = boost::shared_ptr<LutModel> (new LutModel(rgb_cs) );
    }
    else if(systCoulstring == "TSL")
    {
         lutmodel_ptr = boost::shared_ptr<LutModel> (new LutModel(hsl_cs) );
    }
    else
    {
        std::cout << " wrong att systemeCouleur " << std::endl;
        return lutmodel_ptr;
    }
    std::string typeInterpstring = pElem->Attribute("typeInterpolation");
    if(typeInterpstring == "Interpolation exacte")
    {
       lutmodel_ptr->setInterpolatorType(BoundInterpolator::min);
    }
    else if(typeInterpstring == "Interpolation lineaire")
    {
          lutmodel_ptr->setInterpolatorType(BoundInterpolator::linear);
    }
    else
    {
        std::cout << " wrong att typeInterpolation "<< std::endl;
        return lutmodel_ptr;
    }
   //---child bound nodes
    TiXmlElement *elem = hdl.FirstChildElement().FirstChildElement().Element();
    if(!elem){
       std::cout << " node doesn't exist " << std::endl;
    }
    while (elem){
        std::string d1;
        double nam;
        d1 = elem->Attribute("valeur");
        nam = atof(d1.c_str());
        int channel1,channel2,channel3;
        if(lutmodel_ptr->getColorSpace()== rgb_cs)
        {
                elem->QueryIntAttribute("Rouge", &channel1);
                elem->QueryIntAttribute("Vert", &channel2);
                elem->QueryIntAttribute("Bleu", &channel3);
        }
        else if (lutmodel_ptr->getColorSpace()== hsl_cs)
        {
                elem->QueryIntAttribute("Teinte", &channel1);
                elem->QueryIntAttribute("Saturation", &channel2);
                elem->QueryIntAttribute("Luminance", &channel3);
       }
       // std::cout<<" addBound "<<channel1<<" "<<channel2
       //            <<" "<<channel3<<std::endl;
         lutmodel_ptr->addBound(nam,channel1,channel2, channel3 );
         elem = elem->NextSiblingElement(); // iteration
    }

    return lutmodel_ptr;
}

void
LutIgnXmlFile::saveAs( const boost::shared_ptr<LutModel> lut_ptr ,
                       std::string filename)
{
    //  creation d'un nouveau document xml
    TiXmlDocument doc;
    //  ecriture de l'en-tête
    TiXmlElement* msg;
    TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
    doc.LinkEndChild( decl );
    //ecriture de <lut> lié avec </lut>
    TiXmlElement * root = new TiXmlElement( "lut" );
    doc.LinkEndChild( root );
    if(lut_ptr->getInterpolatorType()==BoundInterpolator::min)
     {
        root->SetAttribute("typeInterpolation","Interpolation exacte");
    }
    else if (lut_ptr->getInterpolatorType()==BoundInterpolator::max)
    {
        root->SetAttribute("typeInterpolation","Interpolation exacte");
    }
    else if (lut_ptr->getInterpolatorType()==BoundInterpolator::linear)
    {
        root->SetAttribute("typeInterpolation","Interpolation lineaire");
    }
    if(lut_ptr->getColorSpace()==rgb_cs)
    {
        root->SetAttribute("systemeCouleur","RVB");
    }
    else if (lut_ptr->getColorSpace()==hsl_cs)
    {
        root->SetAttribute("systemeCouleur","TSL");
    }
    //ecriture des lignes contenant <borne valeur = "" Rouge = "" Vert = "" bleu = "" />
//    for(int C17 = 0;C17<intervalle_lut.size();C17++)
//    {
//        TiXmlElement * window;
//        window = new TiXmlElement( "Borne" );
//        root->LinkEndChild( window );
//        window->SetAttribute("valeur",intervalle_lut[C17]->b);
//        if(systemeCouleur==0)
//        {
//        window->SetAttribute("Rouge",intervalle_lut[C17]->Coul.C1);
//        window->SetAttribute("Vert", intervalle_lut[C17]->Coul.C2);
//        window->SetAttribute("Bleu", intervalle_lut[C17]->Coul.C3);
//        }
//        else if(systemeCouleur==1)
//        {
//            window->SetAttribute("Teinte",intervalle_lut[C17]->Coul.C1);
//            window->SetAttribute("Saturation", intervalle_lut[C17]->Coul.C2);
//            window->SetAttribute("Luminance", intervalle_lut[C17]->Coul.C3);
//        }
//    }
//
    doc.SaveFile(filename.c_str() ); // enregistrement du fichier exporté
}
